package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.vo.Room;

/**
 * 订单子项数据操作类
 * 
 * @author peng
 * 
 */
public class RoomUtil {

	public static Room getRoomById(String id) {
		Cursor cursor = SQLUtil.query(SQLUtil.ROOM, null, "id=?",
				new String[] { id }, null, null, null);
		Room room = null;
		if (cursor.moveToFirst()) {
			do {				
				room = new Room();
				room.setId(cursor.getInt(0));
				room.setName(cursor.getString(1));
				room.setBrief(cursor.getString(2));
				room.setTableNum(cursor.getInt(3));
				room.setHasAircondition(cursor.getInt(4));
				room.setHasBathroom(cursor.getInt(5));
				room.setHasMahjong(cursor.getInt(6));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return room;
	}

	public static ArrayList<Room> getAllRooms() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery(
				"SELECT * FROM " + SQLUtil.ROOM, null);
		ArrayList<Room> rooms = new ArrayList<Room>();
		if (cursor.moveToFirst()) {
			do {
				Room room = new Room();
				room.setId(cursor.getInt(0));
				room.setName(cursor.getString(1));
				room.setBrief(cursor.getString(2));
				room.setTableNum(cursor.getInt(3));
				room.setHasAircondition(cursor.getInt(4));
				room.setHasBathroom(cursor.getInt(5));
				room.setHasMahjong(cursor.getInt(6));
				rooms.add(room);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return rooms;
	}

	public static long insertRoom(Room room) {
		ContentValues values = new ContentValues();		
		values.put("id", room.getId());
		values.put("name", room.getName());
		values.put("brief", room.getBrief());
		values.put("tableNum", room.getTableNum());
		values.put("hasAircondition", room.getHasAircondition());
		values.put("hasBathroom", room.getHasBathroom());
		values.put("hasMahjong", room.getHasMahjong());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.ROOM, null,
				values);
	}

	public static long updateRoom(Room room) {
		ContentValues values = new ContentValues();
		values.put("id", room.getId());
		values.put("name", room.getName());
		values.put("brief", room.getBrief());
		values.put("tableNum", room.getTableNum());
		values.put("hasAircondition", room.getHasAircondition());
		values.put("hasBathroom", room.getHasBathroom());
		values.put("hasMahjong", room.getHasMahjong());
		return SQLUtil.getWritableDatabase().update(SQLUtil.ROOM, values,
				"id=?", new String[] { String.valueOf(room.getId()) });
	}

	public static void deleteRoom(Room room) {
		SQLUtil.getWritableDatabase().delete(SQLUtil.ROOM, "id=?",
				new String[] { String.valueOf(room.getId()) });
	}
	

	public static void deleteAllRooms() {
		SQLUtil.getWritableDatabase().delete(SQLUtil.ROOM,null,null);
	}
}
